package com.photovoltaic.neo4j.controller;

import com.photovoltaic.neo4j.entity.Company;
import com.photovoltaic.neo4j.service.GraphAnalysisService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/neo4j/companies")
@RequiredArgsConstructor
public class CompanyController {
    private final GraphAnalysisService graphAnalysisService;

    @GetMapping("/search")
    public ResponseEntity<?> searchCompanies(@RequestParam String keyword) {
        try {
            List<Company> results = graphAnalysisService.searchCompanies(keyword);
            // 添加调试日志
            return ResponseEntity.ok(Map.of(
                "data", results.stream()
                    .map(c -> Map.of(
                        "id", c.getId() == null ? "" :  c.getId().toString(),
                        "name", c.getName()
                    ))
                    .collect(Collectors.toList())
            ));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of(
                "message", "搜索服务暂不可用",
                "detail", e.getMessage()
            ));
        }
    }
} 